<script setup>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router"
import request from '@/api/request'
import { Search } from '@element-plus/icons-vue'
let router = useRouter();
const rules = reactive({

})

let purchaseReturns = ref({})
let formRef = ref()
let purchaseOrder=ref([])
let warehouse=ref([])
let User=ref([])


let isVisible = ref(false)
let title = ref("")
let data = ref({})

let purchase_returns = ref([])

let page = ref({
    current: 1,
    size: 10000,
    total: 0,
    records: []
})

let toRemove = () => {
    request({
        //查询客户
        url:"/purchasseReturns",
        method:"delete",
        data:purchase_returns.value
    }).then(res=>{
        location()
    })
}
//修改
let toUpdate = (row) => {
    title.value = "修改"
    isVisible.value = true;
    data.value = row
    router.push("/procurement/PurchaseReturns/update");
}

let refTanle = () => {
    isVisible.value = false;
    location();
}
//重置
let shuaxin = () => {
    formRef.value.resetFields();
}
//新增
let toInsert = () => {
    title.value = "申请退货";
    isVisible.value = true;
    router.push("/procurement/PurchaseReturns/insert");
}

//选择框事件
let fuxuankuang = (a) => {
    purchase_returns.value = a
}

let location = () => {
    request({
        //查询员工
            url:"/system/user/list",
            method:"post",
            data:{
                current:1,
                size:1000
            }
        }).then(res=>{
            User.value=res.data.records
        })
     request({
        //查询采购订单
        url:"/purchaseOrder",
        method:"get"
    }).then(res=>{
        purchaseOrder.value=res.data.records
    })
    request({
        //查询退货记录
        url:"/purchasseReturns",
        method:"get",
        params:{
            ...purchaseReturns.value
    }
    }).then(res=>{
        page.value=res.data
    })
    request({
        //查询仓库
        url:"/warehouse",
        method:"get",
        params:{
            current:1,
            size:1000
    }
    }).then(res=>{
        warehouse.value=res.data.date
    })
}

location();

let isUpdate=(row)=>{

    if(row=="无需退货"){
        return true
    }
    return false;
}
</script>

<template>
    <div>
        <el-card>
            <template #header>
                <el-form ref="formRef" inline :model="purchaseReturns" style="display: flex;justify-content: start;"
                    class="card_wrap">
                    <el-form-item label="退货单号" prop="purchaseReturnsId" class="c_item">
                        <el-input v-model="purchaseReturns.purchaseReturnsId" class="sousuo"
                            placeholder="请输入单号"></el-input>
                    </el-form-item>
                    <el-form-item label="采购单" prop="purchaseReturnsPurchaseOrderId" class="c_item">
                        <el-select class="sousuo" v-model="purchaseReturns.purchaseReturnsPurchaseOrderId" @change="location">
                            <el-option label="全部" value="全部">
                                </el-option>
                            <el-option v-for="item in purchaseOrder" :label="item.purchaseOrderName" :value="item.purchaseOrderId">
                                </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="退货仓库" prop="purchaseReturnsReturnsWarehouse" class="c_item">
                        <el-select class="sousuo" v-model="purchaseReturns.purchaseReturnsReturnsWarehouse" @change="location">
                             <el-option label="全部" value="全部">
                                </el-option>
                            <el-option v-for="item in warehouse" :label="item.warehouseName" :value="item.warehouseId">
                                </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="确认退货" prop="purchaseReturnsConfirm" class="c_item">
                        <el-select class="sousuo" v-model="purchaseReturns.purchaseReturnsConfirm" @change="location">
                            <el-option label="全部" value="全部">
                            </el-option>
                            <el-option label="已确认" value="已确认">
                            </el-option>
                            <el-option label="无需退回" value="无需退回">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="退货时间" prop="purchaseReturnsReturnTime" class="c_item">
                        <el-date-picker v-model="purchaseReturns.purchaseReturnsReturnTime" value-format="YYYY-MM-DD" type="date"
                            placeholder="选择日期时间" @change="location">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="经办人" prop="purchaseReturnsAttn" class="c_item">
                        <el-select class="sousuo" v-model="purchaseReturns.purchaseReturnsAttn" @change="location">
                            <el-option label="全部" value="全部">
                                </el-option>
                            <el-option v-for="item in User" :label="item.nickname" :value="item.id">
                                </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item class="c_item">
                        <template #default>
                            <el-button type="primary" @click="location">搜索</el-button>
                            <el-button type="info" :plain=true @click="shuaxin">重置</el-button>
                            <p></p>
                        </template>
                    </el-form-item>
                </el-form>

                <div style="margin: 20px 0px;">
                    <el-button @click="toInsert" type="primary" :plain="true">+添加</el-button>
                    <el-button type="danger" :plain=true @click="toRemove" style="border:0px"
                        :disabled="!purchase_returns.length">删除</el-button>
                </div>

                <el-table :data="page.records" :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }"
                    @selection-change="fuxuankuang" show-overflow-tooltip>
                    <el-table-column type="selection" width="55" />
                    <el-table-column type="expand">
                        <template #default="props">
                            <div m="4" style="margin: 0 120px;padding: 10px 0;" v-if="props.row.returnDetailsList">
                                <el-table :data="props.row.returnDetailsList" :border="false"  :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }">
                                    <el-table-column prop="returnDetailsId" label="退货详情编码" />
                                    <el-table-column prop="returnDetailsInboundDetailsId" label="入库详情编码"
                                        />
                                    <el-table-column prop="returnDetailsNumberOfReturns" label="退货数量"
                                       />
                                    <el-table-column prop="returnDetailsNoNumberOfReturns" label="退货数量(未入库)"
                                      />
                                    <el-table-column prop="returnDetailsSupplierPricesId" label="供应商价格编码"
                                       />
                                    <el-table-column prop="returnDetailsPurchaseReturnsId" label="采购退货编码"
                                        :align="'left'" header-align="left" />
                                </el-table>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="purchaseReturnsId" label="退货单编码" width="150px;" />
                    <el-table-column prop="purchaseReturnsPurchaseOrderId" label="采购单编码" width="150px;" />
                    <el-table-column prop="warehouse.warehouseName" label="退货仓库" width="150px;" />
                    <el-table-column prop="purchaseReturnsNumberOfReturns" label="退货数量" width="150px;" />
                    <el-table-column prop="purchaseReturnsReturnPrice" label="退货金额" width="150px;" />
                    <el-table-column prop="purchaseReturnsConfirm" label="确认退货" width="145px;" #default="scope">
                        <el-tag :type="scope.row.purchaseReturnsConfirm == '无需退货' ? 'primary' : 'danger'">{{
                            scope.row.purchaseReturnsConfirm}}</el-tag>
                    </el-table-column>
                    <el-table-column prop="purchaseReturnsReturnTime" label="退货时间" width="160px;" />
                    <el-table-column prop="nickname" label="经办人" width="140px;" />
                    <el-table-column #default="scope" label="操作" width="140px;" fixed="right">
                        <el-button type="primary" v-if="isUpdate(scope.row.purchaseReturnsConfirm)" @click="toUpdate(scope.row)" :row="scope.row">修改</el-button>
                    </el-table-column>
                </el-table>

                <el-dialog :title="title" v-model="isVisible" :destroy-on-close="true" width="60%">
                    <router-view @ref-table="refTanle" :data="data">
                    </router-view>
                </el-dialog>
            </template>

        </el-card>

    </div>
</template>

<style scoped>
.sousuo {
    width: 200px;
    /* margin-right: 100px; */
}

.card_wrap {
    width: 98%;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
}

.c_item {
    flex: 1;
    margin: 0px 5px 15px 0;
    width: calc((100%-10px)/3);
    min-width: calc((100% - 10px)/3);
    max-width: calc((100% - 10px)/3);

    &:nth-child(3n) {
        margin-right: 0;
    }
}

[m='4'] :deep(.el-table) {
    --el-table-border-color: transparent;
}

:deep(.el-tooltip):hover {
    cursor: pointer;
}
</style>